package it.lol.stats;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class GetStatsServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4665794815940237536L;
	private static final String lolking = "http://www.lolking.net/";

	public void doGet (HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
		
		String summoner_number = req.getParameter("summonername");
		String summoner_server = req.getParameter("server");
		
		List<String> match_history = getStatsFromLolKing(summoner_number, summoner_server);
		List<Match> matches = getListOfMatchesPlayed(match_history);
		
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Key summoner_key = KeyFactory.createKey("Stats", summoner_number);
		
		Query query = new Query("Stats", summoner_key).addSort("date", Query.SortDirection.DESCENDING);
	    List<Entity> stats = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
	    
	    // if there are entities we will get the last 10 matches played 
	    // and check if they are saved or not.
	    if (!stats.isEmpty()) {
	    	for (int i = 0; i < matches.size(); )
	    		i++;
	    }
	}
	
	private List<String> getStatsFromLolKing (String summoner_number, String summoner_server) {
		List<String> match_history = new ArrayList<String>();
		
		try {
            URL url = new URL(lolking + summoner_server + "/" + summoner_number);
 
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            
            String line;
            while ((line = reader.readLine()) != null && !line.contains("-- MATCH HISTORY --")) {
                
            }
            
            
            
            while ((line = reader.readLine()) != null && !line.contains("-- MASTERIES --")) {
                match_history.add(line);
            }
            
            reader.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        }
		
		return match_history;
		
	}
	
	private List<Match> getListOfMatchesPlayed (List<String> match_history) {
		List<Match> matches = new ArrayList<Match>();
		
		
		
		return matches;
	}

	
	class Match {
		String champion;
		int kills;
		int assists;
		int deaths;
		int creeps;
		int gold;
		Date date;
		boolean victory;
		String[] items;
		
		Match(String c, int k, int a, int d, int cs, int g, Date _d, boolean v, String[] it) {
			champion = c;
			kills = k;
			assists = a;
			deaths = d;
			creeps = cs;
			gold = g;
			date = _d;
			victory = v;
			items = it;
		}
		
	}
}
